package com.campus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.entity.ResultBody;
import com.campus.entity.Schedule;
import com.campus.entity.vo.ScheduleVO;
import com.campus.service.ScheduleService;
import com.campus.mapper.ScheduleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author LuTanhang
 * @description 针对表【schedule】的数据库操作Service实现
 * @createDate 2023-04-01 14:42:17
 */
@Service
public class ScheduleServiceImpl extends ServiceImpl<ScheduleMapper, Schedule> implements ScheduleService {


    @Autowired
    private ScheduleMapper scheduleMapper;

    /**
     * 辅助方法，通过传递的时间戳，得到这一周开始的时间戳和结束的时间戳
     *
     * @return
     */
    private static long[] getCurrentWeekTimeFrame(long time) {
        //
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        //start of the week
        if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
            calendar.add(Calendar.DAY_OF_YEAR, -1);
        }
        calendar.add(Calendar.DAY_OF_WEEK, -(calendar.get(Calendar.DAY_OF_WEEK) - 2));
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        long startTime = calendar.getTimeInMillis();
        //end of the week
        calendar.add(Calendar.DAY_OF_WEEK, 6);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        long endTime = calendar.getTimeInMillis();
        return new long[]{startTime, endTime};
    }

    @Override
    public ResultBody query(String account, long time) {
        long[] currentWeekTimeFrame = getCurrentWeekTimeFrame(time);
        Timestamp beginWeek = new Timestamp(currentWeekTimeFrame[0]);
        Timestamp endWeek = new Timestamp(currentWeekTimeFrame[1]);
        List<Schedule> schedules = scheduleMapper.selectList(new QueryWrapper<Schedule>().eq("account", account).le("end_time", endWeek).ge("begin_time", beginWeek));
        List<ScheduleVO> scheduleVOS = new ArrayList<>();
        for (Schedule schedule : schedules) {
            scheduleVOS.add(new ScheduleVO(schedule));
        }
        schedules.sort(Comparator.comparing(Schedule::getBeginTime));
        Map<String, Object> map = new HashMap<>();
        map.put("list", scheduleVOS);
        map.put("beginTime", beginWeek);
        return ResultBody.ok().data("msg", map);
    }

    @Override
    public ResultBody delSchedule(String id) {
        int i = scheduleMapper.deleteById(Integer.valueOf(id));
        if (i != 0) {
            return ResultBody.ok();
        } else {
            return ResultBody.error();
        }
    }

    @Override
    public ResultBody update(Schedule schedule) {
        int i = scheduleMapper.updateById(schedule);
        if (i == 0) {
            return ResultBody.error();
        } else {
            return ResultBody.ok();
        }
    }

    @Override
    public ResultBody add(Schedule schedule) {
        int insert = scheduleMapper.insert(schedule);
        if (insert != 0) {
            return ResultBody.ok();
        } else {
            return ResultBody.error();
        }
    }

    @Override
    public ResultBody queryInMonth(String account, String year, String month) {

        Date beginMonth = new Date(Integer.parseInt(year) - 1900, Integer.parseInt(month) - 1, 1, 0, 0, 0);
        Date endTime = new Date(Integer.parseInt(year) - 1900, Integer.parseInt(month), 1, 0, 0, 0);
        List<Schedule> schedules = scheduleMapper.selectList(new QueryWrapper<Schedule>().eq("account", account).ge("begin_time", beginMonth).le("end_time", endTime));
        Map<String, List<ScheduleVO>> dates = new HashMap<>();
        for (Schedule schedule : schedules) {
            String key = patternDate(schedule.getBeginTime());
            List<ScheduleVO> voList = dates.computeIfAbsent(key, (k) -> new ArrayList<>());
            voList.add(new ScheduleVO(schedule));
        }
        return ResultBody.ok().data("data", dates);
    }

    private String patternDate(Timestamp time) {
        String res = "y";
        Date date = new Date(time.getTime());
        res += date.getYear() + 1900;
        res += "m";
        res += date.getMonth() + 1;
        res += "d";
        return res + date.getDate();
    }

}




